<?php
class ControllerCatalogReviewim extends Controller {
	private $error = array();

	public function index() {
		$data['user_token'] = $this->session->data['user_token'];
		$data['import'] = $this->url->link('catalog/reviewim/import', 'user_token=' . $this->session->data['user_token'], true);

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('catalog/review_formim', $data));
	}
	
	
	public function import() {
		ini_set('display_errors', 1);
		ini_set('log_errors', 1);
		error_reporting(E_ALL);
		ini_set("memory_limit","-1");
		set_time_limit(0);
        header("Content-type: text/html; charset=utf-8");
		require_once DIR_SYSTEM . 'phpexcel/Classes/PHPExcel.php';
		
		$allowedExts = array("xls", "xlsx", "csv");
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);     // 获取文件后缀名
		// var_dump($extension);
		// var_dump($_FILES);
		// echo exec('whoami');
		// echo '</br>';
		$filePath = '';
		if ($_FILES["file"]["size"] > 4096000){
			echo "文件大小不要超过4M";
			die();
		}
		if (($_FILES["file"]["size"] < 4096000)   // 小于 2 MB  2319018
			&& in_array($extension, $allowedExts)
		) {
			if ($_FILES["file"]["error"] > 0) {
				echo "错误：: " . $_FILES["file"]["error"] . "<br>";
			} else {
				$path=$_SERVER['DOCUMENT_ROOT']."/ueditor/php/upload/image"; //上传路径   
				// 判断当期目录下的 upload 目录是否存在该文件
				// 如果没有 upload 目录，你需要创建它，upload 目录权限为 777
				if (file_exists($path."/" . $_FILES["file"]["name"])) {
					echo $_FILES["file"]["name"] . " 文件已经存在。 ";
				} else {
					//重命名文件名
					$filename = date('YmdHis' . rand(1000, 9999)) . '.xls';
					// 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
					move_uploaded_file($_FILES["file"]["tmp_name"], $path."/" . $filename);
					$filePath = $path."/" . $filename;
				}
			}
		} else {
			echo "非法的文件格式";
			die();
		}
		
		require_once  DIR_SYSTEM . 'phpexcel/Classes/PHPExcel/IOFactory.php';
		require_once  DIR_SYSTEM . 'phpexcel/Classes/PHPExcel/Reader/Excel2007.php';
		 
		$objReader = PHPExcel_IOFactory::createReader('Excel2007');/*Excel5 for 2003 excel2007 for 2007*/
		$objPHPExcel = $objReader->load($filePath); //Excel 路径
		// $sheet = $objPHPExcel->getSheet(0);
		// $highestRow = $sheet->getHighestRow(); // 取得总行数
		// $highestColumn = $sheet->getHighestColumn(); // 取得总列数
		
		/*方法二【推荐】*/
		$objWorksheet = $objPHPExcel->getActiveSheet();        
		$highestRow = $objWorksheet->getHighestRow();   // 取得总行数     
		$highestColumn = $objWorksheet->getHighestColumn();        
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
				$check=array(
					0 =>'广告专员',1 =>'所属业务部门' ,2 =>'域名' ,3 => '订单号' , 4 => '商户订单号' , 5 =>'运单号',6 =>'姓名' , 7 =>'产品名' , 8 =>'内部名', 9 =>'面单名' ,10 =>'总价' ,11 =>'符号',12 =>'属性' ,13 =>'SKU' ,14 =>'电话', 15 =>'邮箱', 16 =>'邮编' , 17 =>'送货地址' , 18 =>'省',19 =>'市' ,20 =>'区' ,21 =>'购买产品数量' ,22 =>'单价', 23 =>'付款方式', 24 =>'来源',25 =>'留言',26 =>'同步时间',27 =>'下单时间' ,28 =>'到货时间' ,29 =>'上线时间',30 =>'签收时间', 31 =>'仓库', 32 =>'订单状态',33 =>'无效原因' ,34 =>'取消原因', 35 =>'验证码状态',36 =>'发货日期',37 =>'物流名称' ,38 =>'物流状态' ,39 =>'结款状态' ,40 =>'重量', 41 =>'IP' ,42 =>'重复信息' 
				);

				
		for ($row = 2;$row <= $highestRow;$row++)         {
			$strs=array();
			//注意highestColumnIndex的列数索引从0开始
			for ($col = 0;$col < $highestColumnIndex;$col++)            {
				$strs[$col] =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
			}
			
			for ($col = 0;$col < $highestColumnIndex;$col++)  {
				$val=(string)$objWorksheet->getCellByColumnAndRow($col, 1)->getValue();
				
				$strsfirst[$col] =(string)$val;
				
			}
		     
			// var_dump($strsfirst);die;
				// if($strsfirst[4]=='网站地址'){
					
						$this->db->query("INSERT INTO `oc_review`(`product_id`, `customer_id`, `author`, `text`, `rating`, `status`, `date_added`, `date_modified`) VALUES  
						('".$strs[0]."','".$strs[1]."','".$strs[2]."', '".$strs[3]."','".$strs[4]."','".$strs[5]."','".$strs[6]."','".$strs[7]."')");
				   
				// }
			
			    sleep(0.5);
			
			
			
		}

      die;
	}

	
}